/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

package org.adroitlogic.axis.logger.demo;

import org.apache.axis.EngineConfiguration;
import org.apache.axis.AxisEngine;
import org.apache.axis.configuration.BasicClientConfig;

import java.net.URL;

public class Client {
    public static void main(String[] args) throws Exception {

        Thread t1 = new Thread(new Runnable() {
            public void run() {
                try {
                    EngineConfiguration cfg = getEngineConfiguration("cfg1");
                    SimpleStockQuoteServiceLocator svc = new SimpleStockQuoteServiceLocator(cfg);
                    URL endpoint = new URL("http://localhost:9000/soap/SimpleStockQuoteService");
                    SimpleStockQuoteServiceSoap11BindingStub stub =
                        new SimpleStockQuoteServiceSoap11BindingStub(endpoint, svc);

                    for (int i=0; i<1000; i++) {
                        GetQuoteResponse res = stub.getQuote(new GetQuote("IBM"+i));
                        System.out.println("Response : " + res.getLast());
                        Thread.sleep(5000);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }});
        t1.start();

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                try {
                    EngineConfiguration cfg = getEngineConfiguration("cfg2");
                    SimpleStockQuoteServiceLocator svc = new SimpleStockQuoteServiceLocator(cfg);
                    URL endpoint = new URL("http://localhost:9000/soap/SimpleStockQuoteService");
                    SimpleStockQuoteServiceSoap11BindingStub stub =
                        new SimpleStockQuoteServiceSoap11BindingStub(endpoint, svc);

                    for (int i=0; i<1000; i++) {
                        GetQuoteResponse res = stub.getQuote(new GetQuote("SUN"+i));
                        System.out.println("Response : " + res.getLast());
                        Thread.sleep(5000);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }});
        t2.start();

        t1.join();
        t2.join();

    }

    private static org.apache.axis.EngineConfiguration getEngineConfiguration(String id) {

        java.lang.StringBuffer sb = new java.lang.StringBuffer();
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
        sb.append("<deployment name=\"defaultClientConfig\"\r\n");
        sb.append("xmlns=\"http://xml.apache.org/axis/wsdd/\"\r\n");
        sb.append("xmlns:java=\"http://xml.apache.org/axis/wsdd/providers/java\">\r\n");
        sb.append("<transport name=\"http\" pivot=\"java:org.apache.axis.transport.http.HTTPSender\" />\r\n");
        sb.append("<transport name=\"local\" pivot=\"java:org.apache.axis.transport.local.LocalSender\" />\r\n");
        sb.append("<transport name=\"java\" pivot=\"java:org.apache.axis.transport.java.JavaSender\" />\r\n");
        sb.append(
            "<globalConfiguration>" +
            "  <requestFlow>\n" +
            "   <handler type=\"java:org.adroitlogic.axis.logger.Logger\">" +
            "    <parameter name=\"rootPath\" value=\"/tmp/x/y\"/>" +
            "    <parameter name=\"enableJMX\" value=\"true\"/>" +
            "    <parameter name=\"enabled\" value=\"true\"/>" +
            "    <parameter name=\"identifier\" value=\"" + id + "\"/>" +
            "   </handler>" +
            "  </requestFlow>" +
            "  <responseFlow>" +
            "   <handler type=\"java:org.adroitlogic.axis.logger.Logger\"/>" +
            "  </responseFlow>" +
            "</globalConfiguration>");
        sb.append("</deployment>\r\n");
        org.apache.axis.configuration.XMLStringProvider config =
            new org.apache.axis.configuration.XMLStringProvider(sb.toString());
        return config;
    }
}
